package com.gxa.ssm340.service.impl;

import com.gxa.ssm340.common.ErrorCode;
import com.gxa.ssm340.entity.dto.RegistDTO;
import com.gxa.ssm340.entity.pojo.User;
import com.gxa.ssm340.entity.vo.UserLoginVO;
import com.gxa.ssm340.exception.BizException;
import com.gxa.ssm340.mapper.UserLoginMapper;
import com.gxa.ssm340.service.UserLoginService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

/**
 * @BelongsProject: huanlianchaoshi
 * @Author: Donglu
 * @CreateTime: 2023-10-20  16:49
 */
@Service
@Transactional(rollbackFor = Throwable.class)
public class UserLoginServiceImpl implements UserLoginService {

    @Autowired
    private UserLoginMapper userLoginMapper;

    @Override
    public UserLoginVO login(String phone) throws BizException {


        if( StringUtils.isEmpty(phone)){
            throw new BizException(ErrorCode.NULL_FIELD);
        }

        // 核心业务的实现
        UserLoginVO userLoginVO = userLoginMapper.selectByPhone(phone);
        if (userLoginVO.getPhone()==null){
            throw new BizException(ErrorCode.FAILD_LOGIN);
        }

        // 根据返回类型的要求,如果查询成功就返回目标数据,如果失败,抛出业务异常
        return userLoginVO;
    }

    @Override
    public RegistDTO reg(RegistDTO registDTO) throws BizException {
        // 判断该手机号是否存在
       /* UserLoginVO existsUser = userLoginMapper.selectByPhone(registDTO.getPhone());
        if(existsUser.getPhone()!=null){
            throw new BizException(ErrorCode.WRONG_REG_PHONE);
        }*/
        // 添加
        userLoginMapper.insert(registDTO);
        return registDTO;
    }

    @Override
    public User selectUserByPhone(String phone) {

        User user = userLoginMapper.selectUserByPhone(phone);
        System.out.println("注册"+user);
        return user;
    }

}
